<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分组" prop="group_id">
        <el-select v-model="queryParams.group_id" placeholder="请选择分组" filterable>
            <el-option
              v-for="m in groupOptions"
              :key="m.id"
              :label="m.name"
              :value="m.id"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="代码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.dev_app_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="桌面展示" prop="show_top">
        <el-select v-model="queryParams.show_top" placeholder="请选择桌面展示" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:app:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:app:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
              v-hasPermi="['system:app:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:app:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="应用名称" align="center" prop="name" />
      <el-table-column label="代码" align="center" prop="code">
        <template slot-scope="scope">
          <router-link :to="'/dev/app/form/' + scope.row.id" class="link-type" v-if="scope.row.type == 'form'">
            <span>{{ scope.row.code }}</span>
          </router-link>
          <span v-else>{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dev_app_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="分组" align="center" prop="group_name" />
      <el-table-column label="图标" align="center" prop="icon" />
      <el-table-column label="排序" align="center" prop="sorts" />
      <el-table-column label="权限" align="center" prop="permission" />
      <el-table-column label="桌面展示" align="center" prop="show_top">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.show_top"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:app:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:app:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <!-- 添加或修改应用信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-tabs v-model="tab">
        <el-tab-pane label="基本信息" name="base">
        <el-form-item label="分组" prop="group_id">
          <el-select v-model="form.group_id" placeholder="请选择分组" filterable>
            <el-option
              v-for="m in groupOptions"
              :key="m.id"
              :label="m.name"
              :value="m.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入代码" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.dev_app_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入图标" />
        </el-form-item>
        <el-form-item label="排序" prop="sorts">
          <el-input v-model="form.sorts" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="权限" prop="permission">
          <el-input v-model="form.permission" placeholder="请输入权限" />
        </el-form-item>
        <el-form-item label="接口" prop="api">
          <el-input v-model="form.api" placeholder="请输入数据脚本代码" />
        </el-form-item>
        <el-form-item label="目标连接" prop="links">
          <el-input v-model="form.links" placeholder="请输入目标连接" />
        </el-form-item>
        <el-form-item label="桌面展示" prop="show_top">
          <el-radio-group v-model="form.show_top">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="列表配置" name="list"  v-if="form.type == 'list' || form.type == 'page'">
        <el-form-item label="标题字段" prop="title_field">
          <el-input v-model="form.title_field" placeholder="请输入标题字段" />
        </el-form-item>
        <el-form-item label="右侧字段" prop="right_field">
          <el-input v-model="form.right_field" placeholder="请输入右侧字段" />
        </el-form-item>
        <el-form-item label="详细字段" prop="note_field">
          <el-input v-model="form.note_field" placeholder="请输入详细字段" />
        </el-form-item>
        <el-form-item label="图标字段" prop="img_field">
          <el-input v-model="form.img_field" placeholder="请输入图标字段" />
        </el-form-item>
        </el-tab-pane>
      </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-dialog :open="upload" model="dev_app" @cancel="importCancel" @success="importSuccess" />
  </div>
</template>

<script>
import { apiGet, apiPost, apiPut, apiDelete, apiDs, apiExport, apiDownload } from "@/api/openapi";
import ImportDialog from "@/components/ImportDialog";

export default {
  name: "App",
  components: {
    ImportDialog
  },
  dicts: ['dev_app_type', 'sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      groupOptions: [],
      // 应用信息表格数据
      appList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        group_id: null,
        code: null,
        type: null,
        name: null,
        icon: null,
        sorts: null,
        permission: null,
        api: null,
        links: null,
        show_top: null,
        title_field: null,
        right_field: null,
        note_field: null,
        img_field: null,
        "@alias": "t",
        "@exp": {
        name: 'lk',
            }
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      tab: 'base',
      // 导入参数
      upload: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        group_id: [
          { required: true, message: "分组不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        code: [
          { required: true, message: "代码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        sorts: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        show_top: [
          { required: true, message: "桌面展示不能为空", trigger: "change" }
        ],
        title_field: [
          { required: true, message: "标题字段不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.loadGroup();
  },
  methods: {
    /** 查询应用信息列表 */
    getList() {
      this.loading = true;
      let params = {'@pager': this.pager};
      params['dev_app'] = {...this.queryParams};
      params['dev_group'] = {"@alias":"g", "@join":"g.id = t.group_id", "@column":"name:group_name"};
      apiGet(params).then(response => {
        this.appList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    loadGroup() {
        let params = {};
      params['dev_group'] = this.queryParams;
      params['@pager'] = {page:1, limit:1000};
      apiGet(params).then(response => {
        this.groupOptions = response.data.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        group_id: null,
        code: null,
        type: 'list',
        name: null,
        icon: null,
        sorts: null,
        permission: null,
        api: null,
        links: null,
        show_top: 'Y',
        title_field: null,
        right_field: null,
        note_field: null,
        img_field: null
      };
      this.resetForm("form");
      this.tab = 'base';
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应用信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
        let data = {};
        data['dev_app'] = { id:id};
      apiGet(data).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应用信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
        let data = {
          'dev_app': this.form,
           '@tags': ['dev_app']
         };
          if (this.form.id != null) {
            apiPut(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            apiPost(data).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的应用信息数据？').then(function() {
        let data = {};
        data['dev_app'] = { id:ids, "@exp":{id:"in"}};
        data['@tags'] = ['dev_app'];
        return apiDelete(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload = true;
    },
    importSuccess(msg) {
        this.upload = false;
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
    },
    importCancel() {
        this.upload = false;
    },
    /** 导出按钮操作 */
    handleExport() {
        let params = {};
      params['dev_app'] = {...this.queryParams};
      params['@pager'] = {page:1, limit:10000};
      apiExport(params).then(response => {
        apiDownload(response.msg);
      });
    }
  }
};
</script>
